#include "OptionPanel_FaceDetection.h"
#include <iostream>
OCEANUI::CPanel_FaceDetection::CPanel_FaceDetection(QWidget* parent):CPanelBase("CPanel_FaceDetection",parent)
{
}

OCEANUI::CPanel_FaceDetection::~CPanel_FaceDetection(void)
{
}

bool OCEANUI::CPanel_FaceDetection::init()
{
	setupUi(this);
	bool ConnectResult = QObject::connect(pbRun,SIGNAL(clicked()),this,SLOT(OnbtnRunClick()),Qt::DirectConnection);
	assert(ConnectResult);
	CPanelBase::init();
	return true;
}


void OCEANUI::CPanel_FaceDetection::OnEvent(void* pParam,void* Sender)
{

}

void OCEANUI::CPanel_FaceDetection::OnbtnRunClick()
{
	if ( strContext_ == "Static Face")
		processStaticFace_Internal();
	else if ( strContext_ == "Video Face")
		processVideoFace_Internal();
	 
	
}

void OCEANUI::CPanel_FaceDetection::processStaticFace_Internal()
{
	CvSize sz = cvGetSize(currentImage_);
	IplImage*  hsv_image= cvCreateImage(sz , 8, 3);
	cvCvtColor(currentImage_,hsv_image,CV_RGB2HSV);
	IplImage* hsv_mask = cvCreateImage( sz, 8, 1);
	CvScalar hsv_min = cvScalar(0, 20, 40, 0);
	CvScalar hsv_max = cvScalar(255, 150, 255, 0);


	IplImage* h_plane  = cvCreateImage( cvGetSize(currentImage_), 8, 1 );
	IplImage* s_plane  = cvCreateImage( cvGetSize(currentImage_), 8, 1 );
	IplImage* v_plane  = cvCreateImage( cvGetSize(currentImage_), 8, 1 );
	IplImage* planes[] = { h_plane, s_plane };
	cvCvtPixToPlane( hsv_image, h_plane, s_plane, v_plane, 0 );

	int h_bins = 30, s_bins = 32; 
	CvHistogram* hist;
	{
		int    hist_size[] = { h_bins, s_bins };
		float  h_ranges[]  = { 0, 255 };          // hue is [0,180]
		float  s_ranges[]  = { 0, 255 }; 
		float* ranges[]    = { h_ranges, s_ranges };
		hist = cvCreateHist( 
			2, 
			hist_size, 
			CV_HIST_ARRAY, 
			ranges, 
			1 
			); 
	}
	cvCalcHist( planes, hist, 0, 0 );

	float max_value = 0;
	float* min_value;
	int min_idx[2];
	int max_idx[2];
	cvGetMinMaxHistValue( hist, 0, &max_value, min_idx, max_idx);

	hsv_min = cvScalar(max_idx[0] - 10,max_idx[1] - 10,0,0); 
	hsv_max  =cvScalar(max_idx[0] + 30,max_idx[1] + 30,0,0); 
	cvInRangeS (hsv_image, hsv_min, hsv_max, hsv_mask);

	imageWindow_.setImage(hsv_mask);
	imageWindow_.show();
}

void OCEANUI::CPanel_FaceDetection::processVideoFace_Internal()
{
	std::cout<<"processVideoFace_Internal"<<std::endl;
}